// 定义游戏房间类
const PACKET_PER_SEC = 30;
const SPEED = 1 / 2;
const MIN_X = 335 / 30;
const MAX_X = 1512 / 30;
class GameRoom {
    //构造函数
    constructor() {
        //成员变量
        this.RoomId = 0;
        this.RoundStatus = null;//0进行中 1进球了
        this.RoomStatus = 0;//0空闲 1等待其他玩家 2游戏中
        this.Creator = null;
        this.CurrPlayerCount = 0;
        this.MaxPlayerCount = 2;
        this.GoalCount = [0, 0];
        this.PlayerDataArr = new Array();
        //update instance
        this.UpdateInst = null;
        //phy ball
        this.PhyBall = null;
        //--------------------------
        this.pos_ball = {};
        this.pos1 = {};
        this.pos2 = {};
    }
    goal(chairId) {
        this.GoalCount[chairId]++;
    }
    GetGoalData() {
        return this.GoalCount;
    }
    GetPlayersData() {
        return this.PlayerDataArr;
    }
    GetRoomId() {
        return this.RoomId;
    }
    SetRoomId(RoomId) {
        this.RoomId = RoomId;
    }
    GetGameRoomStatus() {
        return this.RoomStatus;
    }
    SetGameRoomStatus(RoomStatus) {
        this.RoomStatus = RoomStatus;
    }

    //获取创建者
    GetCreator() {
        return this.Creator;
    }
    SetCreator(creator) {
        this.Creator = creator;
    }

    GetCurrPlayerCount() {
        return this.CurrPlayerCount;
    }
    GetMaxPlayerCount() {
        return this.MaxPlayerCount;
    }
    //ball in phy world
    GetPhyBall() {
        return this.PhyBall;
    }
    SetPhyBall(phyBall) {
        this.PhyBall = phyBall;
    }
    SetRoundStatus(roundStatus) {
        this.RoundStatus = roundStatus;
    }
    GetRoundStatus() {
        return this.RoundStatus;
    }
    //类中函数
    DEBUG() {
        return "游戏房间信息: 创建者: " + this.Creator + " 当前人数:" + this.CurrPlayerCount + "/" + this.MaxPlayerCount;
    }

    //加入房间
    //return true false
    JOIN(player) {
        player.test = 1010101010101010101;
        if (player == null) {
            console.log("加入房间失败 room.JOIN(player)未传入参数player");
            return false;
        }
        this.PlayerDataArr.forEach(element => {
            if (element.GetPlayerId() == player.GetPlayerId()) return true;
        });
        // if(this.PlayerDataArr[0].GetPlayerId() == player.getPlayerId()
        //     ||this.PlayerDataArr[1].GetPlayerId() == player.getPlayerId()){
        //     //reconnect
        //     return true;
        // }
        if (this.MaxPlayerCount < this.PlayerDataArr.length) {
            console.log("加入房间失败 到达最大玩家数量 当前玩家数量:" + this.PlayerDataArr.length);
            return false;
        }
        this.PlayerDataArr.push(player);
        this.CurrPlayerCount = this.PlayerDataArr.length;
        player.SetChairId(this.CurrPlayerCount - 1)
        player.SetRoomId(this.RoomId);
        return true;
    }

    //离开房间
    //void
    LEAVE(player) {
        var index = this.PlayerDataArr.indexOf(player);
        this.PlayerDataArr.splice(index, 1);
        this.CurrPlayerCoun = this.PlayerDataArr.length;
    }
    STARTGAME() {
        console.log("room " + this.RoomId + " startgame loop()");
        var self = this;
        setTimeout(() => {
            // self.GAMEOVER.call(self)
        }, 15 * 1000);
        this.UpdateInst = setInterval(function () {
            self.update.call(self);
        }, 1000 / PACKET_PER_SEC);
    }
    /* 
    像房间内发送游戏结束消息
    玩家离开房间频道
    删除这个房间的刚体球
    物理世界关于这个房间的信息删除
    */
    GAMEOVER() {
        io.to(this.RoomId).emit('gameEnd', {});
        console.log("room " + this.RoomId + " gameover loop()");
        clearInterval(this.UpdateInst);
        this.PlayerDataArr.forEach(player => {
            player.PlayerSocket.leave(player.RoomId,
                function (data) {
                    console.log("退出" + data);
                })
            player.RoomId = null
        })
        g_PWInstance.m_World.destroyBody(this.PhyBall);
        g_PWInstance.m_RoomPhyData[this.RoomId] = null;
        delete (g_GameRoomList[this.RoomId]);
    }
    update() {
        //sync ball
        var pos_ball = {};
        var pos1 = {};
        var pos2 = {};
        // this.PhyBall.GetDefinition();

        pos_ball.x = this.PhyBall.getPosition().x * 30;//keyBoll.GetPosition()
        pos_ball.y = (- this.PhyBall.getPosition().y) * 30;
        pos_ball.vol = this.PhyBall.getLinearVelocity();
        pos_ball.angle = this.PhyBall.getAngle();
        // var player1_Body = this.PlayerDataArr[0].body;
        // var player2_Body = this.PlayerDataArr[1].body;
        // //player1
        // if(this.PlayerDataArr[0].GetPlayerIsMoving()){
        //     if(this.PlayerDataArr[0].GetPlayerDirect() == 0){
        //         //left
        //         player1_Body.pos.x -= SPEED;
        //         player1_Body.pos.x = Math.max(player1_Body.pos.x,MIN_X);
        //     }else{
        //         //right
        //         player1_Body.pos.x += SPEED;
        //         player1_Body.pos.x = Math.min(player1_Body.pos.x,MAX_X);
        //     }
        // }
        // if(this.PlayerDataArr[0].GetPlayerIsJumping()){
        //     //jump
        // }
        // pos1.x = this.PlayerDataArr[0].body.GetPosition().x*30;
        // pos1.y = (- this.PlayerDataArr[0].body.GetPosition().y);
        // pos1.dir = this.PlayerDataArr[0].GetPlayerDirect();
        // pos1.bMove = this.PlayerDataArr[0].GetPlayerIsMoving();
        // //player2
        // if(this.PlayerDataArr[1].GetPlayerIsMoving()){
        //     if(this.PlayerDataArr[1].GetPlayerDirect() == 0){
        //         //left
        //         player2_Body.pos.x -= SPEED;
        //     }else{
        //         //right
        //         player2_Body.pos.x += SPEED;
        //     }
        // }
        // if(this.PlayerDataArr[1].GetPlayerIsJumping()){
        //     //jump
        // }
        // pos2.x = this.PlayerDataArr[1].body.GetPosition().x*30;
        // pos2.y = (- this.PlayerDataArr[1].body.GetPosition().y);
        // pos2.dir = this.PlayerDataArr[1].GetPlayerDirect();
        // pos2.bMove = this.PlayerDataArr[1].GetPlayerIsMoving();

        io.to(this.RoomId).emit('pos', { "pos_ball": pos_ball/* , "pos_player": pos1, "pos_player2": pos2 */ });//xxx加入了房间 前端显示
        // this.PlayerDataArr[0].GetPlayerSocket().emit("pos", { "pos_ball": pos_ball, "pos_player": pos1, "pos_player2": pos2 });
        // this.PlayerDataArr[1].GetPlayerSocket().emit("pos", { "pos_ball": pos_ball, "pos_player": pos1, "pos_player2": pos2 });
    }

    //静态函数
    //static sayHello(name){
    //    //修改静态变量
    //    this.para = name;
    //   return 'Hello, ' + name;
    //}

}

//静态变量
//GameRoom.g_roomlist = new Array();
module.exports = GameRoom;